Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
GraphQL query giving a "cannot read properties of undefined error"

I wrote a graphql query to get recent posts for a blog app in NextJs in other to display them as widgets

export const getRecentPosts = async () =\> {
const query = gql`   
 query MyQuery {         
  posts(orderBy: createdAt_ASC
     last: 3   ) 
     {title     
      featuredImage {url}     
      createdAt     
      slug
        }     
      }
`;


const result = await request(graphAPI, query);
return result.posts;
};

but i keep getting a "Cannot read properties of undefined (reading 'document')" error

and it keeps displaying this error

NextJs error

I imported the function into the component

function PostWidget({ categories, slug }) {

    const [relatedPosts, setRelatedPosts] = useState([]);
    useEffect(() => {
        if (slug) {
            getSimilarPosts(categories, slug).then((result) => {
                setRelatedPosts(result);
            });
        } else {
            getRecentPosts().then((result) => {
                setRelatedPosts(result);
            });
        }
    }, [slug]);

    console.log(relatedPosts)
    return (
        <div className="bg-white shadow-lg rounded-lg p-8 mb-8">
            <h3 className="text-xl mb-4 font-semibold border-b p-4">
                {slug ? "Related posts" : "Recent posts"}
            </h3>
            {
                relatedPosts.map((post) => (
                    <div key={post.title} className="flex items-center w-full mb-4">
                        <div className="w-16 flex-none">
                            <img
                                alt={post.title}
                                height="60px"
                                width="60px"
                                className="align-middle rounded-full"
                                src={post.featuredImage.url}
                            />
                        </div>
                    </div>
                ))
            }
        </div>
    )
}

but it gives an empty array when i console.log the related posts

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda